//
//  NormalDataBase.m
//  ProjectA_V0.1
//
//  Created by liuchang on 14/12/31.
//  Copyright (c) 2014年 liuchang. All rights reserved.
//

#import "NormalDataBase.h"
#import "NormalClockModel.h"
#import "DB.h"
@implementation NormalDataBase

+ (void)insertIntoActWithModel:(NormalClockModel *)norModel
{
    sqlite3 * dbpoint = [DB openDB];
    NSString * sqlStr = [NSString stringWithFormat:@"INSERT INTO normalClock VALUES ('%@',%i,%i,%i,%i,%i,%i,%i,%i,%i,'%@')", norModel.time, norModel.isOn, norModel.onlyOnce, norModel.sun, norModel.mon, norModel.tue, norModel.wed, norModel.thu, norModel.fri, norModel.sat, norModel.remark];
    int result = sqlite3_exec(dbpoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"成功");
    }
    else {
        NSLog(@"失败");
    }
}

+ (void)deleteFromNorWithModelTime:(NSString *)time
{
    sqlite3 * dbpoint = [DB openDB];
    NSString * sqlStr = [NSString stringWithFormat:@"DELETE FROM normalClock WHERE time='%@'", time];
    int result = sqlite3_exec(dbpoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"成功");
    }
    else {
        NSLog(@"失败");
    }
}

+ (NSArray *)searchAllModel
{
    sqlite3 * dbpoint = [DB openDB];
    sqlite3_stmt * stmt = nil;
    NSString * sqlStr = @"SELECT * FROM normalClock";
    int result = sqlite3_prepare_v2(dbpoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray * arr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *time = sqlite3_column_text(stmt, 0);
            BOOL isOn = sqlite3_column_int(stmt, 1);
            const unsigned char *remark = sqlite3_column_text(stmt, 10);
            NormalClockModel * p = [[NormalClockModel alloc] init];
            p.time = [NSString stringWithUTF8String:(const char *)time];
            p.isOn = isOn;
            p.remark = [NSString stringWithUTF8String:(const char *)remark];
            [arr addObject:p];
            [p release];
        }
    }
    
    sqlite3_finalize(stmt);
    
    return arr;
}

@end
